package com.lzh.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzh.edu.entity.Chapter;
import com.lzh.edu.entity.Video;
import com.lzh.edu.mapper.ChapterMapper;
import com.lzh.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.edu.service.VideoService;
import com.lzh.edu.vo.course.ChapterVo;
import com.lzh.edu.vo.course.VideoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lizuhu
 * @since 2021-04-24
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    @Override
    public List<ChapterVo> getChapterByCourseId(String courseId) {

        QueryWrapper<Chapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        wrapperChapter.orderByAsc("sort", "id");
        List<Chapter> chapters = baseMapper.selectList(wrapperChapter);

        QueryWrapper<Video> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        wrapperVideo.orderByAsc("sort", "id");
        List<Video> videos = videoService.list(wrapperVideo);

        ArrayList<ChapterVo> chapterVoArrayList = new ArrayList<>();

        //将chapter封装到chapterVo
        int chapterCount = chapters.size();
        for (int i = 0; i < chapterCount; i++) {
            Chapter chapter = chapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setLevel("0");
            BeanUtils.copyProperties(chapter, chapterVo);
            chapterVoArrayList.add(chapterVo);

            //将video封装到videoVo
            ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();
            int videoCount = videos.size();
            for (int j = 0; j < videoCount; j++) {
                Video video = videos.get(j);
                if (chapter.getId().equals(video.getChapterId())) {
                    VideoVo videoVo = new VideoVo();
                    videoVo.setLevel("1");
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoArrayList.add(videoVo);
                }
            }
            //将小节封装到章节中
            chapterVo.setChildren(videoVoArrayList);
        }
        return chapterVoArrayList;
    }

    //根据章节id删除章节
    @Override
    public Boolean deleteChapterById(String chapterId) {
        //先查询章节中是否有小节表
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id", chapterId);
        int count = videoService.count(videoQueryWrapper);
        // System.out.println("============="+count);
        if (count > 0) {
            // System.out.println("不能删除该章节");
        }else {
            baseMapper.deleteById(chapterId);
        }
        return count <= 0;
    }

    @Override
    public Boolean removeCourseById(String id) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", id);
        Integer count = baseMapper.delete(queryWrapper);
        return null != count && count > 0;
    }
}
